<?php
/*
 * Created on 2013-9-11
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
include_once("../configure/configure.php");		//--> global var

set_time_limit(0);

include_once("impl/ContentImpl.php");
include_once("impl/TagImpl.php");
include_once("impl/CategoryImpl.php");

calCategoryTag(0);
calTagContentCount();
calTagImageCount();
calCategoryContentCount();

function calCategoryTag($categoryId)
{
	$count = 0;
	
	$oContImpl 	= new ContentImpl(DB_TAG_MM);
	$oTagImpl 	= new TagImpl(DB_TAG_MM);
	$oCatImpl 	= new CategoryImpl(DB_TAG_MM);

	$catIds 	= $oCatImpl->getLeafIds($categoryId, true);
	
	foreach($catIds as $catId){
		$contentIds = $oContImpl->getContentIds($catId);
		
		foreach($contentIds as $contentId){
			$tagsId = $oContImpl->getImageTagIds($contentId);
			
			foreach($tagsId as $tagId){
				$sql = "INSERT INTO category_tags SET categoryId = $catId, tagId = $tagId ON DUPLICATE KEY UPDATE tagId = VALUES(tagId)";
			
				$count 	+= $oContImpl->update(DB_TAG_MM, $sql);
			}
		}
	}
	
	echo 'update category tag count = ' . $count . "\n";
}

function calCategoryContentCount()
{
	$count = 0;
	
	$oCatImpl = new CategoryImpl(DB_TAG_MM);
	
	$arrContentCount = $oCatImpl->getContentCount();
	
	foreach($arrContentCount as $catId => $dataCount){
		if($catId > 0){
			$sql	= "UPDATE category SET r_dataCount = $dataCount  WHERE id = $catId";
			$count 	+= $oCatImpl->update(DB_TAG_MM, $sql);
		}
	
	}
	
	echo 'update valid category content count = ' . $count . "\n";
	
	$count = 0;
	
	$arrHasContents = $oCatImpl->getHashCount();
	$invalidContentCount = array_diff_key ($arrHasContents, $arrContentCount);
	
	foreach($invalidContentCount as $catId => $contentCount){
		$sql = "UPDATE category SET r_dataCount = 0 WHERE id = $catId";
		
		$count 	+= $oCouponImpl->update(DB_TAG_MM, $sql);
	}
	
//	
//	$arrCategories = $oCatImpl->getCustoms('', '', 0, 1000);
//			
//	foreach($arrCategories as $oCat){
//		$couponCount = $oCatImpl->getCategoryCouponCount($oCat->getId());
//		
//		$sql = "UPDATE category SET r_couponCount = $couponCount WHERE id = " . $oCat->getId();
//		
//		$oCouponImpl->update(DB_TAG_MM, $sql);
//	}
}

function calTagContentCount()
{
	$count = 0;
	
	$oContImpl 	= new ContentImpl(DB_TAG_MM);
	$oTagImpl 	= new TagImpl(DB_TAG_MM);

	$arrTagDataCount 	= $oTagImpl->getTagContentCount(DB_TAG_MM);
	
	foreach($arrTagDataCount as $tagId => $countentCount){
		$sql = "UPDATE tags SET r_ContentCount = $countentCount WHERE id = $tagId";
		
		$count 	+= $oTagImpl->update(DB_TAG_MM, $sql);
	}
	
	echo 'update tag valid content count = ' . $count . "\n";
	
	$count = 0;
	
	$arrHasDataTags 	= $oTagImpl->getHasContentTags(DB_TAG_MM);
	$invalidDataCount = array_diff_key ($arrHasDataTags, $arrTagDataCount);
	
	foreach($invalidDataCount as $tagId => $dataCount){
		$sql = "UPDATE tags SET r_ContentCount = 0 WHERE id = $tagId";
		
		$count 	+= $oTagImpl->update(DB_TAG_MM, $sql);
	}
	
	echo 'update tag invalid content count = ' . $count . "\n";
}

function calTagImageCount()
{
	$count = 0;
	
	$oContImpl 	= new ContentImpl(DB_TAG_MM);
	$oTagImpl 	= new TagImpl(DB_TAG_MM);

	$arrTagDataCount = $oTagImpl->getTagImageCount(DB_TAG_MM);
	
	foreach($arrTagDataCount as $tagId => $dataCount){
		$sql = "UPDATE tags SET r_imageCount = $dataCount WHERE id = $tagId";
		
		$count 	+= $oTagImpl->update(DB_TAG_MM, $sql);
	}
	
	echo 'update tag valid content count = ' . $count . "\n";
	
	$count = 0;
	
	$arrHasDataTags 	= $oTagImpl->getHasImageTags(DB_TAG_MM);
	$invalidDataCount 	= array_diff_key ($arrHasDataTags, $arrTagDataCount);
	
	foreach($invalidDataCount as $tagId => $dataCount){
		$sql = "UPDATE tags SET r_imageCount = 0 WHERE id = $tagId";
		
		$count 	+= $oTagImpl->update(DB_TAG_MM, $sql);
	}
	
	echo 'update tag invalid content count = ' . $count . "\n";
}
?>
